package com.stephen.bp.hexin.controller;

import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.stephen.bp.hexin.entity.Product;
import com.stephen.bp.hexin.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/products")
public class ProductController {

    @Autowired
    private ProductService productService;

    @GetMapping("/list")
    public List<Product> getAllProducts() {
        return productService.list();
    }

    @GetMapping("/get")
    public Product getProductById(Integer id) {
        return productService.getById(id);
    }

    @GetMapping("/getByParent")
    public List<Product> getProductByParentId(Integer id) {
        LambdaQueryWrapper<Product> productWrapperWrapper = new LambdaQueryWrapper<>();
        productWrapperWrapper.eq(Product::getGoodsClassId, id);
        return productService.list(productWrapperWrapper);
    }

    @PostMapping
    public void addProduct(@RequestBody Product product) {
        productService.save(product);
    }

    @PutMapping("/{id}")
    public void updateProduct(@PathVariable Integer id, @RequestBody Product product) {
        product.setId(id);
        productService.updateById(product);
    }

    @DeleteMapping("/{id}")
    public void deleteProduct(@PathVariable Integer id) {
        productService.removeById(id);
    }
}